$(function () {
    obj.init();
});
var obj = new function () {
    'use strict';
    return {
        licenceList: null,
        iType: null,
        bFree: null,
        sKey: null,
        iId: 0,
        init: function () {
            this.licenceList = new xhDataGrid($('#licenceList'), 'licence/getLicenceBind');
            this.licenceList.parseToolbar().init();
            this.listener();
        },
        listener: function () {
            var $this = this;
            //搜索
            $('#bt_search').click(function () {
                $this.iType = $('#licenceType').val();
                $this.bFree = $('#licenceFree').val();
                $this.sKey = $('#licenceKey').val();
                $this.funObj().searchList();
            });

            $this.licenceList.onCheck = function (rowIndex, rowData) {
                if ($this.iId == rowData['iId']) {
                    $this.iId = 0;
                    $this.licenceList.command('unselectRow', rowIndex);
                    return false;
                } else {
                    $this.iId = rowData['iId'];
                }
            };
            $('#bt_modal_confirm').click(function () {
                xhExtra.setBtnNormal($('#bt_modal_confirm'));
                window.top.swal({
                    showCancelButton: true,
                    confirmButtonText: '确认',
                    cancelButtonText: '取消',
                    showLoaderOnConfirm: true,
                    allowOutsideClick: false,
                    title: '绑定许可证',
                    text:'确认绑定？',
                    preConfirm: function() {
                        return new Promise(function () {
                            $.ajax({
                                url: 'bindLicenceDev',
                                data: {id:$this.iId,deviceIds:deviceIds},
                                type: 'POST',
                                success: function (data) {
                                    window.top.xhTop.showSuccess(
                                        data.msg
                                    );
                                    window.top.xhTop.closeModal();
                                },
                                error: function (data) {
                                    xhExtra.setBtnNormal($('#bt_modal_confirm'));
                                    window.top.xhTop.showError(
                                        data.responseJSON.error,
                                        data.responseJSON.msg.msg,
                                    );
                                }
                            });
                        }).catch(swal.noop);
                    }
                });
            });

            $('#bt_modal_close').click(function () {
                window.top.xhTop.closeModal();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                searchList: function () {
                    $this.licenceList.reload({iType: $this.iType, bFree: $this.bFree, sKey: $this.sKey});
                },
            }
        }
    }
};
